package com.sdkx.app.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sdkx.app.R;
import com.sdkx.app.ViewModel.MarkManager;
import com.sdkx.app.dialog.markdialog.MarkTypeDialog;
import com.sdkx.app.model.AlarmPoint;
import com.sdkx.app.util.ToastUtil;
import com.sdkx.app.util.Util;
import com.sdkx.app.view.BrokenLineView;
import com.sdkx.app.view.DegreeInputView;

public class AlarmInfoDialog extends Dialog implements View.OnClickListener{
    AlarmPoint alarmPoint;
    EditText nameEditView,radiusEditView;
    DegreeInputView lonview,latview;
    ImageView typeImage;
    TextView typetext;
    Button btnCancel,btnSave;
    int alarmType;
    ImageView imageStyle;
    int  linewidth;
    int linecolor;
    IAlarmCallback iAlarmCallback;
    public AlarmInfoDialog(@NonNull Context context) {
        super(context);
        init();
    }

    public AlarmInfoDialog(Context context, AlarmPoint point) {
        super(context);
        alarmPoint =  point;
        init();
        nameEditView.setText(point.getName());
        radiusEditView.setText(String.valueOf(point.getRadius()));
        lonview.setTextEx(Util.degreePatten(getContext(),point.getLongitude()));
        latview.setTextEx((Util.degreePatten(getContext(),point.getLatitude())));
       // typeImage.setImageResource(point.type);
        alarmType  = point.type;
        MarkManager.MarkShape shape = MarkManager.getAllMarkShape().get(point.type);
        typetext.setText(shape.shapName);
        imageStyle.setImageBitmap(BrokenLineView.getBitmap(100,100,alarmPoint.getLineWidth(),alarmPoint.getLinecolor()));
        linecolor = alarmPoint.getLinecolor();
        linewidth = alarmPoint.getLineWidth();
    }

    public void init(){
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawable(new ColorDrawable(getContext().getResources().getColor(android.R.color.transparent)));
        View view  = getLayoutInflater().inflate(R.layout.alarm_aire_info_layout,null);
        this.setContentView(view);
        nameEditView = view.findViewById(R.id.name);
        radiusEditView = view.findViewById(R.id.radius);
        lonview = view.findViewById(R.id.lon);
        latview = view.findViewById(R.id.lat);
        typeImage = view.findViewById(R.id.type);
        typetext = view.findViewById(R.id.typetext);
        imageStyle = view.findViewById(R.id.linestyle);
        imageStyle.setOnClickListener(this);
        view.findViewById(R.id.cancel).setOnClickListener(this);
        view.findViewById(R.id.save).setOnClickListener(this);
        view.findViewById(R.id.typeparent).setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.save:
                String name  = nameEditView.getText().toString();
                if(TextUtils.isEmpty(name)){
                    ToastUtil.show(getContext(),"名称不能为空",Gravity.CENTER);
                    return;
                }
                String lat  = latview.getText().toString();
                if(TextUtils.isEmpty(lat)){
                    ToastUtil.show(getContext(),"纬度不能为空",Gravity.CENTER);
                    return;
                }
                String lon = lonview.getText().toString();
                if(TextUtils.isEmpty(lon)){
                    ToastUtil.show(getContext(),"经度不能为空",Gravity.CENTER);
                    return;
                }
                String radius = radiusEditView.getText().toString();

                if(TextUtils.isEmpty(radius) ){
                    ToastUtil.show(getContext(),"半径不能为空，且必须是数字",Gravity.CENTER);
                    return;
                }
                if(alarmPoint == null){
                    alarmPoint = new AlarmPoint();
                }
                alarmPoint.setRadius(Double.valueOf(radius));
                alarmPoint.setName(name);
                alarmPoint.setLongitude(lonview.toDegree());
                alarmPoint.setLatitude(latview.toDegree());
                alarmPoint.setLinecolor(linecolor);
                alarmPoint.setLineWidth(linewidth);
                alarmPoint.setType(alarmType);
                alarmPoint.save();
                if(iAlarmCallback != null){
                    iAlarmCallback.onCallback(alarmPoint);
                }
                break;
            case R.id.cancel:
                dismiss();
                break;
            case R.id.typeparent:
                final MarkTypeDialog markTypeDialog = new MarkTypeDialog(getContext());
                markTypeDialog.setOnSelectListener(new MarkTypeDialog.IMarkTypeListener() {
                    @Override
                    public void onSelect(int position) {
                        Toast.makeText(getContext(), "marktype = " + position, Toast.LENGTH_SHORT).show();
                        MarkManager.MarkShape shape = MarkManager.getAllMarkShape().get(position);
                        typeImage.setImageResource(shape.drawableId);
                        //String info = wpname.getText().toString();
                        typetext.setText(shape.shapName);
                        alarmType = position;
                        markTypeDialog.dismiss();
                    }
                });
                markTypeDialog.show();
                break;
            case R.id.linestyle:
                LineColorDialog dialog  = new LineColorDialog(getContext());
                if(alarmPoint != null){
                    dialog.setColor(alarmPoint.getLinecolor());
                    dialog.setLineWidth(alarmPoint.getLineWidth());
                }else{
                    if(linecolor != 0) {
                        dialog.setColor(linecolor);
                        dialog.setLineWidth(linewidth);
                    }
                }
                dialog.setColorCallback(new LineColorDialog.ICallback() {
                    @Override
                    public void onCallBack(int color, int width, Bitmap bitmap) {
                        linecolor = color;
                        linewidth = width;
                        imageStyle.setImageBitmap(bitmap);
                        if(alarmPoint != null ){
                            alarmPoint.setLineWidth(width);
                            alarmPoint.setLinecolor(color);
                        }
                    }
                });
                dialog.show();
                break;
        }
    }
    public interface IAlarmCallback{
         void onCallback(AlarmPoint point);
    }
    public void setCallback(IAlarmCallback callback){
        iAlarmCallback = callback;
    }
}
